//
//  WBMainViewController.swift
//  weibo
//
//  Created by Felix on 16/11/3.
//  Copyright © 2016年 Felix. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //1.设置tabbar选中颜色
        tabBar.tintColor = UIColor.orange;
        
        //2.添加子控制器
        addChildViewControllers()
    }
    
    //加载发微博按钮并设置位置
    //放在viewWillAppear中是因为放在viewDidLoad中会被tabbar覆盖、使得button不能响应点击，因为在viewDidLoad中界面不一定加载完成
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.addSubview(compousButton)
        let rect = compousButton.frame.size
        
        let width = tabBar.frame.size.width / CGFloat(self.childViewControllers.count)
        
        //1.设置按钮位置
        compousButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: rect.height)
        //2.设置偏移量。会不居中
//        compousButton.frame = compousButton.frame.offsetBy(dx: 2 * width, dy: 0)
        
    }
    
    
   //MARK:- 添加所以子控制器
    func addChildViewControllers() {
        
        guard let path = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else {
            AdaLog(message: "文件不存在")
            return
        }
        
        guard let data = NSData.init(contentsOfFile: path) else {
            AdaLog(message: "加载二进制数据失败")
            return
        }
        
        do{
            /// 由于这个方法会抛出catch 所以要用do catch 捕获异常并做处理
            let objc = try JSONSerialization .jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as![[String:Any]]
            
            for dict in objc {
                let vcString = dict["vcName"] as? String
                let vcTitle = dict["title"] as? String
                let vcImage = dict["imageName"] as? String
                addChildViewController(childControllerName: vcString, title: vcTitle, image: vcImage)
                
            }
            
        }catch{
            //1.添加首页
            addChildViewController(childControllerName:"WBHomeViewController", title: "首页", image: "tabbar_home")
            
            //2.添加消息中心
            addChildViewController(childControllerName: "WBMessageCenterViewController", title: "消息", image: "tabbar_message_center")
            
            //4.添加发现
            addChildViewController(childControllerName: "WBNullViewController", title: "", image: "")
            
            //4.添加发现
            addChildViewController(childControllerName: "WBDiscoverViewController", title: "发现", image: "tabbar_discover")
            
            //5.添加个人中心
            addChildViewController(childControllerName: "WBProfileViewController", title: "我", image: "tabbar_profile")
        }
        
        
    }
    
    
    //重载系统方法,根据传入的参数添加子控制器
    func addChildViewController(childControllerName :String? , title:String? , image:String?) {
        
        
        //1.获取命名空间
        guard let name = Bundle.main.infoDictionary!["CFBundleExecutable"] as?String else {
            AdaLog(message: "获取命名空间失败")
            return
        }
        
       //2.获取类的正确名称(命名空间.类名)
        var clsVC:AnyClass? = nil
        if let vcName = childControllerName{
            let fullName:String = name.appending(".").appending(vcName)
            clsVC = NSClassFromString(fullName)
        }
        
        //3.判断是否是UITableViewController类
        guard let clsType = clsVC as? UITableViewController.Type else {
            AdaLog(message: "clsVC不是UITableViewController类型")
            return;
        }
        
        //4.创建类
        let childController =  clsType.init()
        
        
        //5.设置类的title
        childController.title = title;
        
        //6.解包图片名
        guard let imageName = image else {
            AdaLog(message: "图片地址错误")
            return
        }
        //7.解包成功、设置tabbar图片
        childController.tabBarItem.image = UIImage.init(named: imageName)
        childController.tabBarItem.selectedImage = UIImage.init(named: imageName.appending("_highlight"))
        let nav = UINavigationController.init(rootViewController: childController)
        
        //8.加载控制器到UITabbarViewControllerz中
        addChildViewController(nav)
    }
  
    
    //发微博按钮点击
    //加上@objc是为了让方法支持oc一样的方法动态派发以达到privite的权限控制
    @objc private func composeClick() -> Void {
        AdaLog(message: "发微博按钮点击")
    }
    
    
    //MARK:-懒加载发微博按钮
    lazy var compousButton:UIButton = {
        () -> UIButton in

        
        
        let btn = UIButton.init(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
        
        btn.addTarget(self, action: #selector(WBMainViewController.composeClick), for: UIControlEvents.touchUpInside)
        
        
        return btn
        
    }()
}
